package top.yunmouren.webui.Engine;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;

import static javafx.stage.StageStyle.TRANSPARENT;
@Environment(EnvType.CLIENT)
public class Engine extends Application {
    public static WebView browser;
    public static Stage stage;
    public static WebEngine webEngine;
    public static String Url;
    public MinecraftClient mc = MinecraftClient.getInstance();
    public int Width = mc.getWindow().getWidth();
    public int Height = mc.getWindow().getHeight();


    @Environment(EnvType.CLIENT)
    public void init() {
        Platform.setImplicitExit(false);

    }
    @Environment(EnvType.CLIENT)
    public void onInitializeClient(){
        browser = new WebView();
        webEngine = browser.getEngine();
    }
    @Environment(EnvType.CLIENT)
    public void start(Stage window) {
        stage = window;
        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root, Width, Height);
        onInitializeClient();
        ScrollPane scrollPane = new ScrollPane(browser);
        browser.setMinWidth(scene.getWidth());
        browser.setMinHeight(scene.getHeight());
        scrollPane.setPrefSize(scene.getWidth(), scene.getHeight());
        root.getChildren().add(scrollPane);
        webEngine.load(Url);
        window.setScene(scene);
        window.setResizable(false);
        window.initStyle(TRANSPARENT);
        window.setTitle("Web UI");

        window.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
            if (event.getCode() == KeyCode.ESCAPE) {
                Platform.exit();
            }
        });

        window.show();
    }
    @Environment(EnvType.CLIENT)
    public void stop() {
        Platform.exit();
    }
    @Environment(EnvType.CLIENT)
    public void main() {
        launch();
    }
}
